	package com.niuwa.p2p.service.resource.impl;
	
	import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
	


	import org.springframework.beans.factory.annotation.Autowired;
	


	import com.niuwa.p2p.dao.resource.ResourcesDao;
import com.niuwa.p2p.entity.resource.Resource;
import com.niuwa.p2p.service.resource.ResourcesService;
	
	public class ResourcesServiceImpl implements ResourcesService {
		@Autowired
		private ResourcesDao resourcesDao;
	
		// public Object getResource(){
		// List<Resource> resources=resourceDao.getResource();
		// TreeNode nodes=new TreeNode(AppConstants.ROLE_ROOT_ID, "角色列表") ;//根节点
		// nodes.setState("open");
		// nodes.setChildren(getJsonByList(resources,Integer.parseInt(AppConstants.ROLE_ROOT_ID)));
		// return nodes;
		// }
	
		/**
		 * @see 递归得到所有目录,并存放事先封装好的树类
		 * @param parentId
		 *            父节点ID
		 * @return
		 */
	
		/*
		 * private List<TreeNode> getJsonByList(List<Resource> list, Integer
		 * parentId){ List<TreeNode> nodeList = new ArrayList<TreeNode>(); for
		 * (Resource resource : list) { if (resource.getPid().equals(parentId)) {
		 * TreeNode tn = new TreeNode(); tn.setId(String.valueOf(resource.getId()));
		 * tn.setTitle(resource.getName()); List<TreeNode> treeNodes=new
		 * LinkedList<TreeNode>(); treeNodes = getJsonByList(list,
		 * resource.getId()); if (!treeNodes.isEmpty()) { tn.setState("open");
		 * tn.setChildren(treeNodes); }else{ tn.setState("leaf"); }
		 * nodeList.add(tn); } } return nodeList; }
		 */
	
		/**
		 * 根据角色ID 查找权限对应的资源
		 * 
		 * @return
		 */
		@Override
		public List<Resource> getResourceByRoleId(Integer roleId) {
			return resourcesDao.searchResourcesByRoleId(roleId);
		}
	
		/**
		 * 查询所有角色对应的资源
		 * 
		 * @return
		 */
		public List<HashMap<String, Object>> searchResources() {
	
			return resourcesDao.searchResources();
	
		}
	
		/**
		 * 获取所有资源
		 */
		public List<Resource> searchAllResources() {
	
			return resourcesDao.searchAllResources();
		}
	
		@Override
		public List<Resource> searchResourcesByUserInfo(
				HashMap<String, String> paramMap) {
	
			return resourcesDao.searchResourcesByUserInfo(paramMap);
		}
	
		/**
		 * 查询角色对应的不同资源
		 * 
		 * @return
		 */
		public List<String> searchDistinctResource() {
	
			return resourcesDao.searchDistinctResource();
	
		}
	
		/**
		 * 
		 * 为资源授权
		 * 
		 * @param authorityParam
		 *
		 */
		@Override
		public void updateAuthorize(String roleId, String[] resourceNoArr,String[] unCheckResourceNoArr) {
		     List<Resource>	 resourceList =getResourceByRoleId(Integer.parseInt(roleId));
			List<String> resourceNoList = new ArrayList<String>();
			for (Resource resource : resourceList) {
				resourceNoList.add(resource.getNumber());
			}
			// 根据角色ID删除对应的权限
			if(unCheckResourceNoArr!=null){
			for (String UncheckresourceNo : unCheckResourceNoArr) {
				if (resourceNoList.contains(UncheckresourceNo)) {
					HashMap<String, Object> roleResouceParamMap = new HashMap<String, Object>();
					roleResouceParamMap.put("roleId", Integer.parseInt(roleId));
					Integer resourceId = resourcesDao.searchResourceIdByResourceNo(UncheckresourceNo);
					roleResouceParamMap.put("resourceId", resourceId);
					resourcesDao.deleteRoleResouce(roleResouceParamMap);
				}
			}
		}
			// 增加角色相对应的资源
			if(resourceNoArr!=null){
			for (String resourceNo : resourceNoArr) {
				if (!(resourceNoList.contains(resourceNo))) {
					HashMap<String, Object> roleResouceParamMap = new HashMap<String, Object>();
					roleResouceParamMap.put("roleId", Integer.parseInt(roleId));
					Integer resourceId = resourcesDao.searchResourceIdByResourceNo(resourceNo);
					roleResouceParamMap.put("resourceId", resourceId);
					resourcesDao.insertRoleResouce(roleResouceParamMap);
				}
			  }
			}
		}

		@Override
		public List<Resource> searchAuthResourcesByUserInfo(HashMap<String, String> paramMap) {
	         return resourcesDao.searchAuthResourcesByUserInfo(paramMap);
		}
	}
